import type { Executor } from '../';
import type { ResourceDto } from '../model/dto';
import type { ResourceInput } from '../model/static';

export class ResourceController {
    
    constructor(private executor: Executor) {}
    
    async add(options: ResourceControllerOptions['add']): Promise<void> {
        let _uri = '/add';
        return (await this.executor({uri: _uri, method: 'POST', body: options.body})) as void
    }
    
    async findIdsByRoleId(options: ResourceControllerOptions['findIdsByRoleId']): Promise<
        ReadonlyArray<number>
    > {
        let _uri = '/findIdsByRoleId';
        let _separator = _uri.indexOf('?') === -1 ? '?' : '&';
        let _value: any = undefined;
        _value = options.roleId;
        if (_value !== undefined && _value !== null) {
            _uri += _separator
            _uri += 'roleId='
            _uri += encodeURIComponent(_value);
            _separator = '&';
        }
        return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<number>
    }
    
    async myButtons(): Promise<
        ReadonlyArray<string>
    > {
        let _uri = '/myButtons';
        return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<string>
    }
    
    async myMenus(): Promise<
        ReadonlyArray<ResourceDto['ResourceShape/TREE_FETCHER']>
    > {
        let _uri = '/myMenus';
        return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<ResourceDto['ResourceShape/TREE_FETCHER']>
    }
    
    async tree(): Promise<
        ReadonlyArray<ResourceDto['ResourceShape/TREE_FETCHER']>
    > {
        let _uri = '/tree';
        return (await this.executor({uri: _uri, method: 'GET'})) as ReadonlyArray<ResourceDto['ResourceShape/TREE_FETCHER']>
    }
    
    async update(options: ResourceControllerOptions['update']): Promise<void> {
        let _uri = '/update';
        return (await this.executor({uri: _uri, method: 'POST', body: options.body})) as void
    }
}

export type ResourceControllerOptions = {
    'add': {readonly body: ResourceInput},
    'findIdsByRoleId': {readonly roleId: number},
    'myButtons': {},
    'myMenus': {},
    'tree': {},
    'update': {readonly body: ResourceInput}
}